package edu.mse.camel.client.posterboard.ui.editpart;

import org.eclipse.draw2d.ConnectionLayer;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.Request;
import org.eclipse.gef.editpolicies.RootComponentEditPolicy;
import org.eclipse.gef.editpolicies.SnapFeedbackPolicy;

import com.cea.papyrus.core.directedit.LabelDirectEditPolicy;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.ContainerDimensionAdaptationEditPolicy;
import com.cea.papyrus.core.editpart.ContainerWithSemanticEditPolicy;
import com.cea.papyrus.core.editpart.DimensionAdaptationEditPolicy;
import com.cea.papyrus.core.editpart.GraphNodeEditPolicy;
import com.cea.papyrus.core.editpart.LayoutEditPolicy;
import com.cea.papyrus.diagram.classdiagram.editpart.ClassDiagramEditPart;
import com.cea.papyrus.diagraminterchange2.di2.DiConstants;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;

import edu.mse.camel.client.posterboard.posterboard.PBDiagram;
import edu.mse.camel.client.posterboard.ui.editpart.policy.PBClassLayoutEditPolicy;
import edu.mse.camel.client.posterboard.ui.editpart.policy.PBDiagramComponentEditPolicy;
import edu.mse.camel.client.posterboard.ui.util.EditpartUtil;

/**
 * @author darpan
 *
 */
public class AdaptedClassDiagramEditpart extends ClassDiagramEditPart 
						implements IPosterBoardDiagramEditpart {

	protected PBDiagram pbDiagram;
	
	protected EditpartListener listener;
	
	public AdaptedClassDiagramEditpart(PBDiagram pbDiagram, ModelManager modelManager) {
		super((Diagram) pbDiagram.getDelegate(), modelManager);
		this.pbDiagram = pbDiagram;
		setModel(pbDiagram.getDelegate());
		listener = new EditpartListener(this);
	}

	@Override
	protected IFigure createFigure() {
		return EditpartUtil.createFigure(getModel(), 
				(ConnectionLayer) getLayer(LayerConstants.CONNECTION_LAYER));
	}

	@Override
	protected void createEditPolicies() {
		anchorMngr = getAnchorMngr();
		this.installEditPolicy( EditPolicy.DIRECT_EDIT_ROLE, 
				new LabelDirectEditPolicy() );
		//manage connection between this element
		this.installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, 
				new GraphNodeEditPolicy() );
		installEditPolicy(
				EditPolicy.CONTAINER_ROLE
				, new ContainerWithSemanticEditPolicy());
		installEditPolicy(
			DimensionAdaptationEditPolicy.DIMENSION_ADAPTATION
				, new ContainerDimensionAdaptationEditPolicy());
		installEditPolicy("Snap Feedback", new SnapFeedbackPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new PBDiagramComponentEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new PBClassLayoutEditPolicy(
				(XYLayout) getContentPane().getLayoutManager()));
	}

	public PBDiagram getPbDiagram() {
		return pbDiagram;
	}

	@Override
	public void activate() {
		if (this.isActive()) {
			return;
		}
		super.activate();
		pbDiagram.eAdapters().add(listener);
	}
	
	@Override
	public void deactivate() {
		if (!this.isActive()) {
			return;
		}
		super.deactivate();
		pbDiagram.eAdapters().remove(listener);
	}
	
	public Diagram getCastedModel() {
		return (Diagram) getModel();
	}
	
	@Override
	public void refreshVisuals() {
		super.refreshVisuals();
		refreshSourceConnections();
		refreshTargetConnections();
		EditpartUtil.refreshVisuals(getCastedModel(), this);
	}

	@Override
	protected void fireSelectionChanged() {
		super.fireSelectionChanged();
		EditpartUtil.diagramSelectionChanged(this);
	}
	
	@Override
	public DragTracker getDragTracker(Request req){
		return EditpartUtil.getDragTracker(req, this);
	}
	
	public String getType() {
		return DiConstants.CLASS_DIAGRAM;
	}
	
}
